<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <title>Register</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <style>
        body {
            background: #f3f4f6;
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .register-box {
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            width: 320px;
        }

        .register-box h2 {
            text-align: center;
            margin-bottom: 1.5rem;
        }

        .register-box input {
            width: 100%;
            padding: 0.5rem;
            margin-bottom: 1rem;
            border: 1px solid #ccc;
            border-radius: 8px;
        }

        .register-box button {
            width: 100%;
            padding: 0.6rem;
            background: #10b981;
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
        }

        .register-box button:hover {
            background: #059669;
        }

        .message {
            font-size: 0.9rem;
            text-align: center;
            margin-top: 0.5rem;
        }

        .success {
            color: green;
        }

        .error {
            color: red;
        }
    </style>
</head>

<body>

    <div class="register-box">
        <h2>Register</h2>
        <input type="text" id="username" placeholder="Username" />
        <input type="password" id="password" placeholder="Password" />
        <input type="password" id="confirm" placeholder="Confirm Password" />
        <div class="message" id="message"></div>
        <button onclick="handleRegister()">Register</button>
    </div>

    <script>
        async function handleRegister() {
            const username = document.getElementById("username").value.trim();
            const password = document.getElementById("password").value.trim();
            const confirm = document.getElementById("confirm").value.trim();
            const msgDiv = document.getElementById("message");

            if (!username || !password) {
                msgDiv.textContent = "Username and password required";
                msgDiv.className = "message error";
                return;
            }

            if (password !== confirm) {
                msgDiv.textContent = "Passwords do not match";
                msgDiv.className = "message error";
                return;
            }

            try {
                const res = await fetch("/register", {
                    method: "POST",
                    headers: { "Content-Type": "application/json" },
                    body: JSON.stringify({ username, password }),
                });

                const data = await res.json();
                if (res.ok && data.success) {
                    msgDiv.textContent = data.message || "Registration successful";
                    msgDiv.className = "message success";

                    // 保存 token 到 localStorage（或 sessionStorage）
                    if (data.token) {
                        localStorage.setItem("token", data.token);
                    }

                    // 可跳转页面，如 window.location.href = "/login.html";
                    window.location.href = "/index.html";
                } else {
                    msgDiv.textContent = data.message || "Registration failed";
                    msgDiv.className = "message error";
                }
            } catch (err) {
                msgDiv.textContent = "Network error";
                msgDiv.className = "message error";
            }
        }
    </script>

</body>

</html>